/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "SnapshotGeneModel.h"

#include "SnapshotGeneCollection.h"

/**
**/
SnapshotGeneModel::SnapshotGeneModel() : m_genes( NULL )
{
}

/**
**/
SnapshotGeneModel::~SnapshotGeneModel()
{
}

/**
**/
void SnapshotGeneModel::setGenes( SnapshotGeneCollection *genes )
{
	m_genes = genes;
	connect( m_genes, SIGNAL(dataChanged()), this, SLOT(updateData()) );
}

/**
**/
QModelIndex SnapshotGeneModel::index( int row, int column, const QModelIndex &parent ) const
{
	if( !m_genes || column < 0 || column > 1 )
		return QModelIndex();

	if( parent.isValid() )
		return QModelIndex();

	if( row < 0 || row >= m_genes->getNumGenes() )
		return QModelIndex();

	return createIndex( row, column );
}

/**
**/
QModelIndex SnapshotGeneModel::parent( const QModelIndex &index ) const
{
	return QModelIndex();
}

/**
**/
int SnapshotGeneModel::rowCount( const QModelIndex &parent ) const
{
	if( !m_genes || parent.isValid() )
		return 0;

	return m_genes->getNumGenes();
}

/**
**/
int SnapshotGeneModel::columnCount( const QModelIndex & parent ) const
{
	return 2;
}

/**
**/
Qt::ItemFlags SnapshotGeneModel::flags( const QModelIndex &index ) const
{
	if( index.column() == 0 )
		return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;

	return Qt::ItemIsEnabled;
}

/**
**/
QVariant SnapshotGeneModel::data( const QModelIndex &index, int role ) const
{
	if( !m_genes || !index.isValid() )
		return QVariant();

	if( role == Qt::CheckStateRole && index.column() == 0 ){
		if( m_genes->getGene( index.row() )->isSelected() )
			return QVariant( Qt::Checked );

		return QVariant( Qt::Unchecked );
	}

	if( role != Qt::DisplayRole )
		return QVariant();

	if( index.column() == 0 ) 
		return m_genes->getGene( index.row() )->getName();
	else if( index.column() == 1 )
		return m_genes->getGene( index.row() )->getNumTaxa();

	return QVariant();
}

/**
**/
QVariant SnapshotGeneModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
	if( role != Qt::DisplayRole )
		return QVariant();

	if( section == 0 )
		return tr( "Gene" );
	else if( section == 1 )
		return tr( "# Taxa" );

	return QVariant();
}

/**
**/
bool SnapshotGeneModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
	if( role != Qt::CheckStateRole || index.column() != 0 )
		return false;

	if( value.toInt() == Qt::Checked ){
		m_genes->getGene( index.row() )->setSelected( true );
		return true;
	}

	if( value.toInt() == Qt::Unchecked ){
		m_genes->getGene( index.row() )->setSelected( false );
		return true;
	}

	return false;
}

/**
**/
void SnapshotGeneModel::updateData()
{
	beginResetModel();
	// May have to change how this is done since the data should not be changed before beginResetModel() is called.
	endResetModel();
}

/**
**/
SnapshotGeneProxyModel::SnapshotGeneProxyModel() : m_genes( NULL )
{
	m_sourceModel = new SnapshotGeneModel;
	setSourceModel( m_sourceModel );
	setSortCaseSensitivity( Qt::CaseInsensitive );
}

/**
**/
SnapshotGeneProxyModel::~SnapshotGeneProxyModel()
{
}

/**
**/
void SnapshotGeneProxyModel::setGenes( SnapshotGeneCollection *genes )
{
	m_genes = genes;
	m_sourceModel->setGenes( genes );
	invalidate();
}

/**
**/
bool SnapshotGeneProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
{
	if( !m_genes || sourceParent.isValid() )
		return false;

	if( m_genes->getGene( sourceRow )->getNumTaxa() > 1 )
		return true;

	return false;
}
